package servlet.Jamin;

import net.sf.json.JSONObject;
import pojo.User;
import service.impl.ImpShoppingMemberService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/RegisterServlet")
public class Register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        JSONObject js = new JSONObject();
        UserServiceImpl us = new UserServiceImpl();
        PrintWriter out = response.getWriter();
        ImpShoppingMemberService im = new ImpShoppingMemberService();
        String userAccount = request.getParameter("r_user_account");


        if (us.searchUserByAc(userAccount) == 0) {


            String pwd = request.getParameter("r_password");
            String phoneNumber = request.getParameter("r_phonenumber");
            User user = new User();
            user.setuAccount(userAccount);
            user.setuPassword(pwd);
            user.setuPhoneNumber(phoneNumber);
            int a = us.userRegister(user);
            int ID = us.searchID(userAccount);
            int b = im.creatShoppingCart(ID);
            if (a == 1 && b == 1) {
                js.put("status", 1);
            } else {
                js.put("status", 0);


            }
        } else {
            js.put("status", 0);
        }
        out.print(js);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
